import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex);

const types={
    SET_AUTHENTICATED:"SET_AUTHENTICATED",
    SET_USER:'SET_USER'//设置用户操作
}

const state={
    //是否授权
    isAuthenticated:false,
    user:{}//解析完token后存储到user
}

//获取状态信息
const getters={
    //获取
    isAuthenticated:state=>state.isAuthenticated,
    user:state=>state.user
}

//更改状态信息
const mutations={
    [types.SET_AUTHENTICATED](state,isAuthenticated){
        if(isAuthenticated)state.isAuthenticated=isAuthenticated;
        else state.isAuthenticated=false;
    },

    [types.SET_USER](state,user){
        if(user)state.user=user;
        else state.user={};
    }
}


//异步操作
const actions={
    setAuthenticated:({commit},isAuthenticated)=>{
        commit(types.SET_AUTHENTICATED,isAuthenticated);
    },
    setUser:({commit},user)=>{
        commit(types.SET_USER,user);
    },
    clearCurrentState:({commit})=>{
        commit(types.SET_AUTHENTICATED,false);
        commit(types.SET_USER,null);
    }
}


//解析token
export default new Vuex.Store({
   state,
   getters,
   mutations,
   actions
})